package com.feidee.fd.sml.algorithm.feature

import com.feidee.fd.sml.algorithm.component.feature.{PCAEncoder, PCAEncoderParam}
import com.feidee.fd.sml.algorithm.util.{TestingDataGenerator, ToolClass}
import org.scalatest.FunSuite

/**
  * @Author songhaicheng
  * @Date 2019/3/26 9:58
  * @Description
  * @Reviewer
  */
class PCAEncoderSuite extends FunSuite {
  val paramStr: String =
    """
      |{
      |	"inputCol": "features",
      |	"outputCol": "lowFeatures",
      |	"preserveCols": "label,features",
      | "k": 5
      |}
    """.stripMargin

  val pca = new PCAEncoder
  val param: PCAEncoderParam = pca.parseParam(new ToolClass().encrypt(paramStr))

  test("pca parameter") {
    assert("features".equals(param.inputCol) && "lowFeatures".equals(param.outputCol) &&
      "label,features".equals(param.preserveCols) && param.k == 5)
  }

  test("pca transformation") {
    val data = TestingDataGenerator.sampleLibsvmData
    val res = pca.train(param, data).transform(data)
    // 判断降维后的特征长度是否等于参数设置的
    assert(res.select(param.outputCol)
      .rdd
      .map(_.getAs[org.apache.spark.ml.linalg.Vector](0).toArray)
      .first
      .length == param.k)
  }

}
